package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;

import Communication.Client;
import Communication.Server;
import Entity.Message;
import Entity.Worker;
import HandleDB.WorkerQuery;
import View.LoginView;

public class LoginController implements Serializable {
	private static final long serialVersionUID = 1L;
	LoginView login;
	Client client;
	WorkerQuery Wquer;

	public LoginController(Client client) {
		login = new LoginView();
		this.client = client;

		login.addLoginListeners(new LoginListener());
	}

	public void showWindow() {
		login.showWindow();
	}

	public void hideWindow() {
		login.hideWindow();
	}

	class LoginListener implements ActionListener {

		// Workers main window controller
		WorkerMainMenuController workerMainMenuController;

		int workerStatus;
		int role;

		// Constructor
		public LoginListener() {

			
			workerMainMenuController = new WorkerMainMenuController(client,
					LoginController.this);
		}

		public void actionPerformed(ActionEvent event) {

			// Login button clicked
			if (event.getSource() == login.getLoginButton()) {

				// Check if one or more fields are empty
				if (login.getUsername().isEmpty()
						|| login.getPassword().isEmpty())
					SystemController
							.displayErrorMessage("One or both of the fields are empty");

				// Fields are full
				else {
	
					Worker worker = new Worker();
					try {
						worker=Wquer.getRow(client, login.getUsername(),login.getPassword());
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					

					if (worker.getWorkerID() == null)
						SystemController
								.displayErrorMessage("User doesnt exist in database");
					else {
						// Password is correct
						if (login.getPassword().equals(worker.getPassword())) {
							switch (worker.getStatus()) {
							
							
							case 1: // Already logged in
								SystemController.displayMessage("User already logged-in !");
								break;
							case 0:	try {
									Wquer.setStatusInDB(client, worker.getWorkerID(),1);
								} catch (SQLException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							break;
							}
													
								
									boolean[] availableButtons = new boolean[8];
									workerMainMenuController.enableAll();
									for(int i=0;i<availableButtons.length;i++)										
									{
										availableButtons[i] = false;										
									}								
									
									switch (worker.getRole()) {

									// Cases fill available buttons according to
									// permission
									case 2: // Sales manager 
										availableButtons[3] = true;										
										availableButtons[4] = true;										
										availableButtons[5] = true;										
										availableButtons[6] = true;										
	
										break;
									case 3:	// CEO
										for(int i=0;i<availableButtons.length;i++)										
											availableButtons[i] = true;										
										
										break;
									case 4:	// Customer Service
										availableButtons[1] = true;
										break;
									case 5:	// Relations person
										availableButtons[0] = true;
										availableButtons[2] = true;
										break;
									
									}

									// Open workers main window with buttons
									// according to permission

									// Set the active buttons according to
									// workers permission
									workerMainMenuController.setActiveButtons(availableButtons);

									// Show main worker window
									workerMainMenuController.showWindow();
									hideWindow();
								}
							}
						}
				
						// Password is incorrect
			}else {
							// Decrease and set new number of attempts left to
							// DB
							SystemController.displayErrorMessage("Username or password incorrect !");
							
							

					}
				}

				// Clear fields
				//login.clearPassword();
				//login.clearUsername();
			}

}	
		